package com.cmict.data.infra.push;

import cn.hutool.core.util.StrUtil;
import com.cmict.data.framework.common.enums.ConfKeyEnum;
import com.cmict.data.framework.security.core.util.AESUtil;
import com.cmict.data.framework.security.core.util.RSAUtils;
import com.cmict.data.infra.controller.admin.message.vo.NotifyInfo;
import com.cmict.data.infra.enums.message.NotifyChannelEnum;
import com.cmict.data.system.api.conf.ConfApi;
import com.cmict.data.system.api.conf.dto.ConfSystemDTO;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 通知邮件发布
 *
 * @author Hccake 2020/12/21
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class MailNotifyPusher implements NotifyPusher {

//    private final MailSender mailSender;

//    private final MailProducer mailProducer;

    @Value("${flowable.message.template-url}")
    private String templateUrl;

    @Resource
    private ConfApi confApi;

    /**
     * 当前发布者的推送方式
     *
     * @return 推送方式
     * @see NotifyChannelEnum
     */
    @Override
    public Integer notifyChannel() {
        return NotifyChannelEnum.MAIL.getValue();
    }

    @Override
    public void push(NotifyInfo notifyInfo, List<AdminUserRespDTO> userList) {
        ConfSystemDTO confValueByConfKey = confApi.getConfValueByConfKey(ConfKeyEnum.SYSTEM_EMAIL.getConfKey());
        String[] emails = userList.stream().map(AdminUserRespDTO::getEmail).filter(StrUtil::isNotBlank).toArray(String[]::new);
        try {
            HtmlEmail email = new HtmlEmail();
            // 这里是SMTP发送服务器的名字：163的如下："smtp.163.com"
            email.setHostName(confValueByConfKey.getEmailSMTPService());
            email.setSmtpPort(Integer.valueOf(confValueByConfKey.getEmailSMTPPort()));
//            if (true) {  // isSSL
//                email.setSSLOnConnect(true);
//                email.setSmtpPort(465);
//            }
            // 字符编码集的设置
            email.setCharset("UTF-8");
            // 收件人的邮箱
            email.addTo(emails);
            // 发送人的邮箱以及发件人名称

            email.setFrom(confValueByConfKey.getEmailAccount(), confValueByConfKey.getEmailName());

            // 如果需要认证信息的话，设置认证：用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
            String password = confValueByConfKey.getEmailPassword();
            if(password.startsWith(AESUtil.ENCRYPT_FLAG)){
                email.setAuthentication(confValueByConfKey.getEmailAccount(), AESUtil.decrypt(password));
            }else{
                email.setAuthentication(confValueByConfKey.getEmailAccount(), password);
            }
            // 要发送的邮件主题
            email.setSubject(notifyInfo.getTitle());
            // 要发送的信息，由于使用了HtmlEmail，可以在邮件内容中使用HTML标签
            email.setHtmlMsg(notifyInfo.getContent().replace("../../..",templateUrl));

            String result1 = email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

}
